package study.produceOrConsume;

public class Produce implements Runnable {
	private String flag = "";
	public Produce( String flag )
	{
		this.flag=flag;
	}
	public void run() {
		while (true)
			produce();
	}

	/**
	 * 生产者生产出来的产品交给店员
	 */
	public  void produce() {
		synchronized (flag) {
			
			if (Store.getProduct() >= Store.MAX_PRODUCT) {
				try {
					System.out.println("产品已满,请稍候再生产");
					flag.wait();
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				Store.operate("jiajia");
				System.out.println("生产者生产第" + Store.getProduct() + "个产品.");
				try {
					flag.notifyAll(); // 通知等待区的消费者可以取出产品了
					flag.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				
			}
		}
	}

}
